﻿#region USING STATEMENTS
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Media;
using Fusion.Content;
#endregion

#region CODE BODY
namespace Fusion.Management
{
    public static class FAudio
    {
        //////////////////////////////////////////////////
        // Data Fields
        //////////////////////////////////////////////////
        private static List<GameEffect> SoundEffects = new List<GameEffect>();
        private static List<GameSong> Songs = new List<GameSong>();

        //////////////////////////////////////////////////
        // Member Properties
        //////////////////////////////////////////////////
        public static Single Volume
        {
            get { return MediaPlayer.Volume; }
            set { MediaPlayer.Volume = value; }
        }

        //////////////////////////////////////////////////
        // Public Static Sound Effect Methods
        //////////////////////////////////////////////////
        public static GameEffect AddSoundEffect(String EffectFile)
        {
            string NameOfFile = Path.GetFileName(EffectFile);

            foreach (GameEffect Effect in SoundEffects)
            {
                if (Effect != null)
                {
                    if (Effect.Name.Equals(NameOfFile))
                    {
                        Effect.AddRef();
                        return Effect;
                    }
                }
            }

            int Index = -1;

            if(SoundEffects.Contains(null))
                Index = SoundEffects.IndexOf(null);

            if (Index < 0)
            {
                GameEffect NewEffect = new GameEffect(EffectFile);
                NewEffect.Handle = SoundEffects.Count;
                SoundEffects.Add(NewEffect);
                return NewEffect;
            }
            else
            {
                SoundEffects[Index] = new GameEffect(EffectFile);
                SoundEffects[Index].Handle = Index;
                return SoundEffects[Index];
            }
        }
        public static Int32 AddSoundEffectI(String EffectFile)
        {
            string NameOfFile = Path.GetFileName(EffectFile);

            foreach (GameEffect Effect in SoundEffects)
            {
                if (Effect != null)
                {
                    if (Effect.Name.Equals(NameOfFile))
                    {
                        Effect.AddRef();
                        return SoundEffects.IndexOf(Effect);
                    }
                }
            }

            int Index = -1;

            if (SoundEffects.Contains(null))
                Index = SoundEffects.IndexOf(null);

            if (Index < 0)
            {
                GameEffect NewEffect = new GameEffect(EffectFile);
                NewEffect.Handle = SoundEffects.Count;
                SoundEffects.Add(NewEffect);
                return SoundEffects.IndexOf(NewEffect);
            }
            else
            {
                SoundEffects[Index] = new GameEffect(EffectFile);
                SoundEffects[Index].Handle = Index;
                return Index;
            }
        }
        public static void ReleaseEffect(GameEffect ToDispose)
        {
            ReleaseEffect(ToDispose.Handle);
        }
        public static void ReleaseEffect(Int32 ToDispose)
        {
            if (SoundEffects[ToDispose] != null)
                SoundEffects[ToDispose].Release();
        }
        public static void ReleaseAllEffects()
        {
            foreach (GameEffect Effect in SoundEffects)
                if(Effect != null)
                    Effect.Release();
        }
        public static void ClearEffects()
        {
            foreach (GameEffect Effect in SoundEffects)
            {
                while (Effect.ReferenceCount > 0)
                    Effect.Release();
            }

            SoundEffects.Clear();
        }
        public static void PlaySoundEffect(GameEffect ToPlay)
        {
            ToPlay.Effect.Play(Volume, 0, 0);
        }
        public static void PlaySoundEffect(GameEffect ToPlay, Single Pan, Single Pitch)
        {
            ToPlay.Effect.Play(Volume, Pan, Pitch);
        }

        //////////////////////////////////////////////////
        // Public Static Song Methods
        //////////////////////////////////////////////////
        public static GameSong AddSong(String SongFile)
        {
            string NameOfFile = Path.GetFileName(SongFile);

            foreach (GameSong Song in Songs)
            {
                if (Song != null)
                {
                    if (Song.Name.Equals(NameOfFile))
                    {
                        Song.AddRef();
                        return Song;
                    }
                }
            }

            int Index = -1;

            if (Songs.Contains(null))
                Index = Songs.IndexOf(null);

            if (Index < 0)
            {
                GameSong NewSong = new GameSong(SongFile);
                NewSong.Handle = Songs.Count;
                Songs.Add(NewSong);
                return NewSong;
            }
            else
            {
                Songs[Index] = new GameSong(SongFile);
                Songs[Index].Handle = Index;
                return Songs[Index];
            }
        }
        public static Int32 AddSongI(String SongFile)
        {
            string NameOfFile = Path.GetFileName(SongFile);

            foreach (GameSong Song in Songs)
            {
                if (Song != null)
                {
                    if (Song.Name.Equals(NameOfFile))
                    {
                        Song.AddRef();
                        return Songs.IndexOf(Song);
                    }
                }
            }

            int Index = -1;

            if (Songs.Contains(null))
                Index = Songs.IndexOf(null);

            if (Index < 0)
            {
                GameSong NewSong = new GameSong(SongFile);
                NewSong.Handle = Songs.Count;
                Songs.Add(NewSong);
                return Songs.IndexOf(NewSong);
            }
            else
            {
                Songs[Index] = new GameSong(SongFile);
                Songs[Index].Handle = Index;
                return Index;
            }
        }
        public static void ReleaseSong(GameSong ToDispose)
        {
            ReleaseSong(ToDispose.Handle);
        }
        public static void ReleaseSong(Int32 ToDispose)
        {
            if (Songs[ToDispose] != null)
                Songs[ToDispose].Release();
        }
        public static void ReleaseAllSongs()
        {
            foreach (GameSong Song in Songs)
                if (Song != null)
                    Song.Release();
        }
        public static void ClearSongs()
        {
            foreach (GameSong Song in Songs)
                while (Song.ReferenceCount > 0)
                    Song.Release();

            Songs.Clear();
        }
        public static void PlaySong(GameSong ToPlay)
        {
            if (!ToPlay.IsNullResource)
                MediaPlayer.Play(ToPlay.Song);
        }
        public static void PlaySong(Int32 ToPlay)
        {
            if (ToPlay > -1 && ToPlay < Songs.Count)
                if (!Songs[ToPlay].IsNullResource)
                    MediaPlayer.Play(Songs[ToPlay].Song);
        }
        public static void Pause()
        {
            if (MediaPlayer.State == MediaState.Playing)
                MediaPlayer.Pause();
        }
        public static void UnPause()
        {
            if (MediaPlayer.State == MediaState.Paused)
                MediaPlayer.Resume();
        }
        public static void Stop()
        {
            if (MediaPlayer.State != MediaState.Stopped)
                MediaPlayer.Stop();
        }
    }
}
#endregion

//////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2012 Jeffery Thomas Sventora
//////////////////////////////////////////////////////////////////////////////////////////////